package Listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class MyListener implements ServletContextListener, HttpSessionListener {
    private int count = 0;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        count++;
        ServletContext context = event.getSession().getServletContext();
        context.setAttribute("count", count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        count--;
        ServletContext context = event.getSession().getServletContext();
        context.setAttribute("count", count);
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ServletContext对象被创建了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext对象被销毁了");
    }
}
